<?php

namespace app\console\controller;

use think\Db;
use app\console\controller\Bloom;

/**
 * 状态修改controller
 * */
class State extends Bloom
{
    /**
     *参数
     * */
    protected $data;
    public function __construct()
    {
        parent::__construct();
        if (!$this->request->isAjax()) $this->error('非法操作');
        $this->data = $this->request->post('table_data');
        $this->data = explode(',', $this->data);
    }

    /***
     * 修改状态操作
     * [0] 表名
     * [1] 主键名
     * [2] 主键值
     * [3] 要编辑的字段
     ***/
    public function change_state()
    {
        $state = Db::name($this->data[0])->where([$this->data[1] => $this->data[2]])->value($this->data[3]);
        if ($state == 1) {
            $new_state = 0;
            switch ($this->data[3]) {
                case 'status':
                    $txt = '禁用';
                    break;
                case 'is_show':
                    $txt = '隐藏';
                    break;
                case 'is_top':
                    $txt = '否';
                    break;
                case 'is_rec':
                    $txt = '否';
                    break;
                case 'is_pass':
                    $txt = '待审核';
                    break;
                case 'is_read':
                    $txt = '未读';
                    break;
                case 'settlement':
                    $txt = '未结算';
                    break;
                default:
                    # code...
                    break;
            }

        } else {
            $new_state = 1;
            switch ($this->data[3]) {
                case 'status':
                    $txt = '正常';
                    break;
                case 'is_show':
                    $txt = '正常';
                    break;
                case 'is_top':
                    $txt = '是';
                    break;
                case 'is_rec':
                    $txt = '是';
                    break;
                case 'is_pass':
                    $txt = '已审核';
                    break;
                case 'is_read':
                    $txt = '已读';
                    break;
                case 'settlement':
                    $txt = '已结算';
                    break;
                default:
                    # code...
                    break;
            }

        }
        Db::name($this->data[0])->where([$this->data[1] => $this->data[2]])->update([$this->data[3] => $new_state]);
        $this->success('状态已更新', '', ['new_state' => $new_state, 'txt' => $txt]);
    }

    /***
     * 排序操作
     * [0] 表名
     * [1] 主键名
     * [2] 主键值
     * 【3】 新的排序
     ***/
    public function resort()
    {
        if ($this->data[3] < 1 || $this->data[3] > 99 || !is_numeric($this->data[3])) {
            $this->error('排序应为1-99间的整数');
        }
        Db::name($this->data[0])->where([$this->data[1] => $this->data[2]])->update(['sort' => $this->data[3]]);
        $this->success('排序已更新');
    }


    /***
     * 删除操作单个
     * [0] 表名
     * [1] 主键名
     * [2] 主键值
     * [3] true/false true直接删除  false软删除
     ***/
    public function con_is_del()
    {
        if ($this->data[3] == 'true') {
            Db::name($this->data[0])->where([$this->data[1] => $this->data[2]])->delete();
        } else {
            Db::name($this->data[0])->where([$this->data[1] => $this->data[2]])->update(['is_del' => 1]);
        }

        $this->success('删除成功');

    }

    /***
     * 删除操作多个
     * [0] 表名
     * [1] 主键名
     * [2] true/false true直接删除  false软删除
     ***/
    public function con_is_del_multi()
    {
        $keyids = $this->request->post('keyids/a');
        $keyids = implode(',', $keyids);
        if ($this->data[2] == 'true') {
            Db::name($this->data[0])->where($this->data[1], 'in', $keyids)->delete();
        } else {
            Db::name($this->data[0])->where($this->data[1], 'in', $keyids)->update(['is_del' => 1]);
        }

        $this->success('删除成功');
    }


}
